/*
	Authors: Samuel Thacker, Faiz Rahman, Andy Wu
	Description: A program file which defines the 'format' function.
	Revisions:
		2012/2/02 - added if-else statement in while loop to check if one or more of strings are NULL
		2012/1/30 - Modification to the output.
		2012/1/26 - Creation of this program file.
*/

#include <stdio.h>
#include "format.h"

void format(ILIST * l,const char * fname) /*Takes a linked list and creates a file using it.*/
	{
	IENTRY * ele;
	FILE * ofile;
	ele = l->First;

	ofile = fopen(fname,"w");	

	while(ele) /*Ends when ele is null (end of the list).*/
		{
		if(!(ele->DeInstruction->reg1))
			{
			fprintf(ofile,"	%s\n",ele->DeInstruction->opcode);
			}
		else if(!(ele->DeInstruction->reg2))
			{
			fprintf(ofile,"	%s		%s\n",ele->DeInstruction->opcode,ele->DeInstruction->reg1);
			}
		else if(!(ele->DeInstruction->reg3))
			{
			fprintf(ofile,"	%s		%s,	%s\n",ele->DeInstruction->opcode,ele->DeInstruction->reg1,ele->DeInstruction->reg2);
			}
		else
			{
			fprintf(ofile,"	%s		%s,	%s,	%s\n",ele->DeInstruction->opcode,ele->DeInstruction->reg1,ele->DeInstruction->reg2,ele->DeInstruction->reg3);
			}
		ele = ele->Next;
		}

	fclose(ofile);
	}

/*EOF*/
